package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.Exception.CustomException;
import com.itheima.dao.SetmealDao;
import com.itheima.dao.SetmealDishDao;
import com.itheima.pojo.Setmeal;
import com.itheima.pojo.SetmealDish;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealDao, Setmeal> implements SetmealService {

    @Autowired
    SetmealDao setmealDao;

    @Autowired
    SetmealDishDao setmealDishDao;

    /**
     * 添加套餐
     */
    @Override
    @Transactional(rollbackFor = CustomException.class)
    public boolean delete(List<Long> ids) {
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Setmeal::getId, ids).eq(Setmeal::getStatus, 0);
        int count = setmealDao.delete(wrapper);
//        if (count > 0) {
//            LambdaQueryWrapper<SetmealDish> wrapper2 = new LambdaQueryWrapper<>();
//            wrapper2.in(SetmealDish::getSetmealId, ids);
//            setmealDishDao.delete(wrapper2);
//            return true;
//        }
        if (count <= 0) {
            try {
                throw new CustomException("当前状态不支持删除");
            } catch (CustomException e) {
                throw new RuntimeException(e);
            }

        }

        LambdaQueryWrapper<SetmealDish> wrapper2 = new LambdaQueryWrapper<>();
        wrapper2.in(SetmealDish::getSetmealId, ids);
        setmealDishDao.delete(wrapper2);
        return true;
    }

}


